using Spine.Unity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class SpineAnimTool : MonoBehaviour
{
    float value = 0;
    SkeletonAnimation skeletonAnimation;
    SkeletonGraphic Skeletongraphic;
    public float slow = 0.5f;
    public bool isPlay = false;
    public bool isLoop = true;

    public void Start()
    {
        isPlay = false;
    }

    void Update()
    {
        if (isPlay && skeletonAnimation != null)
        {
            value += Time.deltaTime * slow;
            skeletonAnimation.AnimationState.Tracks.Items[0].TrackTime = value;
            if (value >= skeletonAnimation.AnimationState.Tracks.Items[0].Animation.Duration)
                value = isLoop ? 0f : skeletonAnimation.AnimationState.Tracks.Items[0].Animation.Duration;
        }
        if (isPlay && Skeletongraphic != null)
        {
            value += Time.deltaTime * slow;
            Skeletongraphic.AnimationState.Tracks.Items[0].TrackTime = value;
            if (value >= Skeletongraphic.AnimationState.Tracks.Items[0].Animation.Duration)
                value = isLoop ? 0f : Skeletongraphic.AnimationState.Tracks.Items[0].Animation.Duration;
        }
    }

    public void Play()
    {
        value = 0f;
        isPlay = true;
        skeletonAnimation = GetComponent<SkeletonAnimation>();
        Skeletongraphic = GetComponent<SkeletonGraphic>();
    }

    public void Stop()
    {
        isPlay = false;
    }

}
